package com.shemy.action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.shemy.entity.User;
import com.shemy.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * @Description
 * @Author Dzsom
 * @Date 2019-04-26 17:40
 * @Encoding UTF-8
 * @Version 1.0
 **/
@Controller
@Scope("prototype")
public class UserAction extends ActionSupport implements ModelDriven<User> {
    private static final long serialVersionUID = 1L;

    private User user = new User();

    @Autowired
    private UserService userService;

    public String saveUser(){
        userService.save(user);
        return SUCCESS;
    }

    /**
     * 获取全部用户
     * @author Dzsom
     * @date 2018/4/25 23:13
     * @return java.lang.String
     */
    public String getAllUser(){
        List<User> list=userService.findAll();
        ActionContext.getContext().put("list",list);
        return "toAll";
    }

    public String deleteUser(){
        userService.delete(user.getUserId());
        return SUCCESS;
    }

    public String updateUser(){
        User u = userService.findById(user.getUserId());
        ActionContext.getContext().put("user",u);
        return "update";
    }

    @Override
    public User getModel() {
        return user;
    }
}
